<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    body {
        border: 1px solid #999;
        padding: 200px 0;
        display: flex;
        flex: 1;
        justify-content: center;
    }

    .box {
        display: flex;
        width: 100px;
        height: auto;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        margin: 20px;
        margin-right: 50px;
    }

    .demo {
        border: 1px solid red;
        background-color: aqua;
        height: 100px;
        width: 100px;
        display: inline-block;
    }

    .demo:hover {
        color: white;
    }

    .animation_d {
        position: relative;
        border-radius: 50%;
        animation: myfirst3 2s 1;
        -moz-animation: myfirst3 2s 1;
        -o-animation: myfirst3 2s 1;
        -webkit-animation: myfirst3 2s 1;
    }
    @keyframes myfirst3 {
        0% {
            transform: rotate(0deg);
            left: 0px;
        }

        25% {
            transform: rotate(20deg);
            left: 0px;
        }

        50% {
            transform: rotate(0deg);
            left: 500px;
        }

        55% {
            transform: rotate(0deg);
            left: 500px;
        }

        70% {
            transform: rotate(0deg);
            left: 500px;
            background: #1ec7e6;
        }

        100% {
            transform: rotate(-360deg);
            left: 0px;
        }
    }

    .animation_d2 {
        width: 500px;
        height: 50px;
        position: relative;
        animation: myfirst4 500ms 5;
        -moz-animation: myfirst4 500ms 5;
        -o-animation: myfirst4 500ms 5;
        -webkit-animation: myfirst4 1s linear 5 alternate;
    }

    @keyframes myfirst4{
        0%{
            top:0;
        }
        20%{
            top:5px;
        }
        30%{
            top:0px;
        }
        40%{
            top:5px;
        }
        60%{
            top:0px;
        }
        80%{
            top:5px;
        }
        100%{
            top:0;
        }
    }
    
</style>

<body>
    <input type="button" id="animation_btn" class="btn btn-primary" value="点击实现动画" />
    <div class="box">
        <span>animation @keyframes myfirst</span>
        <div class="demo">
            此处用来测试
        </div>
    </div>
    <div class="box">
        <span>animation @keyframes myfirst2</span>
        <div class="demo animation_d2">
            此处用来测试
        </div>
    </div>
</body>
<script>
    var btn = document.getElementById("animation_btn");
    var demo = document.getElementsByClassName('demo')
    btn.onclick = function () {
        if (demo[0].className.indexOf("animation_d") > -1) {
            btn.value="点击实现动画";
            demo[0].classList.remove("animation_d");
        } else {
            btn.value="点击取消动画";
            demo[0].classList.add("animation_d");
        }
    }
</script>

</html>